package com.shoushuzhitongche.app.view;

import java.util.Map;
import android.content.Intent;
import com.dixiang.framework.BaseApplication;
import com.dixiang.framework.common.QJNetUICallback;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.config.SystemConfig;
import com.dixiang.framework.network.Net;
import com.dixiang.framework.utils.SharedPreferenceUtils;
import com.dixiang.framework.utils.Utils;
import com.google.gson.reflect.TypeToken;
import com.shoushuzhitongche.app.common.DBManager;
import com.shoushuzhitongche.app.utls.CommonUtils;
import com.shoushuzhitongche.app.utls.Constants;

public class MYApplication extends BaseApplication{
	private final static String ACTION = "com.loacaldata.service";
	private static MYApplication instance;
	public static float density ,heightPixels , widthPixels;//依次屏幕密度， 屏幕高度，屏幕宽度
	public static int startheight; //状态栏高度
	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;
		new DBManager(this).removeDatabase();
		checkDataversion();
	}
	
	//TODO
    
	//检测本地数据更新
    private void checkDataversion(){
    	String url = CommonUtils.getDataVersion(getApplicationContext())+"?api="+SystemConfig.API;
		Net.fetch(this, url , "",
						new TypeToken<QjResult<Map<String , Object>>>() {
						}, new QJNetUICallback<QjResult<Map<String , Object>>>(this) {
							@Override
							public void onSuccess(QjResult<Map<String , Object>> result, int flg) {
								if(result != null && result.getResults() != null){
									String them =String.valueOf(SharedPreferenceUtils.getInstance(instance, Constants.APPINFO).get(Constants.LOACLE_DATA_TIME));
									String value = String.valueOf(result.getResults().get("version"));
									String localdataUrl = String.valueOf(result.getResults().get("localdataUrl"));
									if((!Utils.isStringEmpty(them) && !value.equals(them)) || Utils.isStringEmpty(them)){
										SharedPreferenceUtils.getInstance(instance, Constants.APPINFO).save(Constants.LOACLE_DATA_TIME, value);
										SharedPreferenceUtils.getInstance(instance, Constants.APPINFO).save(Constants.LOACLE_DATA,false);
										
//										if(!Utils.isStringEmpty(them) && !value.equals(them)){
											//启动数据同步服务
											Intent intent = new Intent();
											intent.setAction(ACTION);
											intent.setPackage(getPackageName());
											intent.putExtra(Constants.PARMS.ACTIONURL, localdataUrl);
											startService(intent);
//										}
									}
								}
							}

							@Override
							public void onError(Exception e, QjResult<Map<String , Object>> result,
											String requestFlag) {
								super.onError(e, result, requestFlag);
							}

							@Override
							public void onCompleted(Exception e,
											QjResult<Map<String , Object>> result) {
								super.onCompleted(e, result);
							}
						});
    }
}
